package com.ewanghuiju.app.presenter.home;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.my.MainContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.bean.response.VersionResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.MyHttpResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class 首页
 * @time 2020/2/16
 */
public class MainPresenter extends RxPresenter<MainContract.View> implements MainContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public MainPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getUserInfo() {
        //获取用户信息
        addSubscribe(mDataManager.getUserInfo()
                        .compose(RxUtil.<MyHttpResponse<UserInfoResponBean>>rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<UserInfoResponBean>>(mView) {
                            @Override
                            public void onNext(Optional<UserInfoResponBean> userInfo) {
                                if (mView != null) {
                                    if (userInfo.getIncludeNull() != null) {
                                        mDataManager.insertUserInfoResponBean(userInfo.getIncludeNull());
                                        //刷新信息
                                        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_TO_REFRESH_DATA));
                                    }
//                            mView.showUserInfo(userInfo.getIncludeNull());
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                                if (mView != null)
//                                    mView.showUserInfoError();
                            }
                        })
        );
    }

    @Override
    public void getUpgradeInfo(String version, String type) {
        //获取更新信息
        addSubscribe(mDataManager.getUpgradeInfo(version, type)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<VersionResponBean>>(mView, true, false) {
                    @Override
                    public void onNext(Optional<VersionResponBean> optional) {
                        mView.showUpgradeInfo(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void attachView(MainContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 获取用户信息   --刷新用户信息
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_GET_USERINFO;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        getUserInfo();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }
}
